/*
 * Copyright © 2009 Perseus Project - Tufts University <http://www.perseus.tufts.edu>
 *
 * This file is part of UniCollatorPerseus.
 *
 * UniCollatorPerseus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * UniCollatorPerseus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with UniCollatorPerseus.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.himeros.text;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.RuleBasedCollator;
import java.text.ParseException;

/**
 * Provides a {@link RuleBasedCollator}, reading collation rules from a file.
 * Example:<br/>
 * <code>RuleBasedCollator rbc=UniCollator.makeRuleBasedCollator("grcCollationRules.txt")</code><br/>
 * <code>Collections.sort(vector,rbc)</code><br/>
 *
 * @author Federico Boschetti <federico.boschetti.73@gmail.com>
 */
public class UniCollator{
    public UniCollator(){}

    /**
     *
     * @param collationRulesFile the File containing the rules for collation
     * @return a new RuleBasedCollator
     */
    public static RuleBasedCollator makeRuleBasedCollator(String collationRulesFile){
        RuleBasedCollator rbc=null;
        try{
            BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(collationRulesFile), "UTF-8"));
            StringBuffer sb=new StringBuffer();
            String line=null;
            while((line=br.readLine())!=null){
                if(line.startsWith("//")) continue;
                sb.append(line);
            }
            rbc=new RuleBasedCollator(sb.toString());
            br.close();
        }catch(IOException ioEx){
            ioEx.printStackTrace();
        }catch(ParseException parseEx){
            parseEx.printStackTrace();
        }
        return rbc;
    }

}
